require "import"
import "str"

--this.setDebug(true)

local json=require"cjson"
local config=require"widget_config/oneword"
:new(this)

local id=0
local added
local initSettings

--local textSize=config:getTextSize(id)
local isAutoDark=config.isAutoDark()

local function styleClick(i)
if id==0 then
toast("无效的微件 ID")
return
end
if i==0 then
config:setStyle(id,0)
elseif i==1 then
config:setStyle(id,isAutoDark&&2||1)
else
config:setStyle(id,2)
end
initSettings()
config:setPendingSentence(
id,config:getSentence(id)
)
this.sendBroadcast(
Intent()
.setAction("android.appwidget.action.APPWIDGET_UPDATE")
.setClass(this--[[.getApplication()]],
luajava.bindClass"com.windmill.widget.OneWordWidgetProvider"
)
.putExtra("appWidgetIds",int{id})
)
end

local data
data={
{__type=5,
title={},
source={},
background={},
},
{__type=1,
title="外观",
},
{__type=3,
style1={
onClick=function()
styleClick(0)
end,
},
card1={
--background=圆角(nil,文字色,{20,20,20,20,20,20,20,20}),
},
preview1={
text="预览",
textColor=isAutoDark&&文字色||0xff000000
},
title1={
text=isAutoDark&&"跟随系统"||"白",
},
style2={
onClick=function()
styleClick(1)
end,
},
card2={
--background=圆角(nil,文字色,{20,20,20,20,20,20,20,20}),
},
preview2={
textColor=isAutoDark&&文字色||-1,
text="预览",
},
title2={
text=isAutoDark&&"透明"||"黑",
},
style3={
onClick=function()
styleClick(2)
end,
visibility=isAutoDark&&8||nil,
},
card3={
--background=圆角(nil,文字色,{20,20,20,20,20,20,20,20}),
},
preview3="预览",
title3={
text="透明"
},
},
{__type=4,
title="字体大小",
--action=textSize.." px",
progress={
--progress=2,
onSeekBarChangeListener=SeekBar.OnSeekBarChangeListener{
onProgressChanged=function(v,p,i)
if !i then
return
elseif id==0 then
toast("无效的微件 ID")
return
end
local d--=data[3]
for _,v in ipairs(data) do
if v.__type==4 then
d=v
break
end
end
--this.setDebug(true)
--print(p)
d.progress.progress=p
local rp=p+14
d.action=rp.." px"
v.getParent().getChildAt(0)
.getChildAt(1)
.setText(d.action)
config:setTextSize(id,rp)
--initSettings()
local f=list.getChildAt(0)
--print(f)
if luajava.instanceof(f,FrameLayout) then
local s=f.getChildAt(1)
if s &&luajava.instanceof(s,SeekBar) then
f=f.getChildAt(0)
f.getChildAt(0).setTextSize(2,rp)
f.getChildAt(1).setTextSize(2,rp-2)
end
end
end
}
},
},
{__type=6,
title="点击时",
action="无",
},
{__type=2,
title="微件外观可能会因您的启动器设置而有所不同。\
\
除了通过点按微件或在应用内刷新微件，系统或驱动器还可能会根据电量或有关设置刷新微件。",
},

}

local adp=LuaMultiAdapter(this,data,{
{--1
TextView,
textColor=文字色,
clickable=true,
id="title",
textSize="14sp",
layout_width=-1,
padding="16dp",
},
{--2
TextView,
textColor=次要文字色,
id="title",
clickable=true,
textSize="14sp",
layout_width=-1,
padding="16dp",
},
{--3
LinearLayout,
layout_width=-1,
clickable=true,
padding="16dp",
paddingLeft="10dp",
--clickable=false,
{
LinearLayout,
orientation=1,
layout_width=-1,
layout_weight=1,
id="style1",
layout_marginLeft="6dp",
{
FrameLayout,
id="card1",
layout_width=-1,
{
TextView,
id="preview1",
layout_width=-1,
foreground=波纹(波纹色,圆角(nil,-1,{20,20,20,20,20,20,20,20})),
backgroundDrawable=圆角(nil,
isAutoDark&&背景色||-1,
{20,20,20,20,20,20,20,20}
),
layout_margin=3,--"2dp",
gravity=17,
textSize="16sp",
padding="16dp",
textColor=文字色,
},
},
{
TextView,
id="title1",
layout_width=-1,
gravity=17,
padding="6dp",
textSize="14sp",
textColor=文字色,
},
},
{
LinearLayout,
layout_weight=1,
orientation=1,
id="style2",
layout_marginLeft="6dp",
layout_width=-1,
{
FrameLayout,
id="card2",
layout_width=-1,
{
TextView,
id="preview2",
layout_width=-1,
backgroundDrawable=圆角(nil,
isAutoDark&&背景色||0xff000000,
{20,20,20,20,20,20,20,20}
),
layout_margin=3,--"2dp",
gravity=17,
textSize="16sp",
padding="16dp",
foreground=波纹(波纹色,圆角(nil,-1,{20,20,20,20,20,20,20,20})),
},
},
{
TextView,
layout_width=-1,
id="title2",
gravity=17,
padding="6dp",
},
},
{
LinearLayout,
layout_weight=1,
layout_width=-1,
layout_marginLeft="6dp",
id="style3",
orientation=1,
{
FrameLayout,
layout_width=-1,
id="card3",
{
TextView,
id="preview3",
layout_width=-1,
foreground=波纹(波纹色,圆角(nil,-1,{20,20,20,20,20,20,20,20})),
layout_margin=3,--"2dp",
gravity=17,
textColor=文字色,
textSize="16sp",
padding="16dp",
backgroundDrawable=圆角(nil,背景色,{20,20,20,20,20,20,20,20}),
},
},
{
TextView,
layout_width=-1,
gravity=17,
id="title3",
padding="6dp",
},
},
},
{--4
LinearLayout,
layout_width=-1,
padding="16dp",
orientation=1,
clickable=true,
{
LinearLayout,
layout_width=-1,
gravity=17,
{
TextView,
layout_weight=1,
layout_width=-1,
id="title",
textSize="16sp",
textColor=文字色,
},
{
TextView,
id="action",
textColor=次要文字色,
layout_marginLeft="8dp",
textSize="16sp",
},
},
{
SeekBar,
id="progress",
layout_width=-1,
max=4,
layout_marginTop="2dp",
},
},
{--5
FrameLayout,
padding="16dp",
clickable=true,
layout_width=-1,
{
LinearLayout,
layout_width=-1,
padding="10dp",
orientation=1,
gravity=17,
minimumHeight="156dp",
id="background",
{
TextView,
layout_gravity=17,
id="title",
},
{
TextView,
layout_gravity=5,
layout_marginTop="4dp",
id="source",
}
},
},
{--6
LinearLayout,
layout_width=-1,
padding="16dp",
gravity=17,
{
TextView,
layout_weight=1,
layout_width=-1,
id="title",
textSize="16sp",
textColor=文字色,
},
{
TextView,
textSize="16sp",
textColor=文字色,
id="action",
},
},
{--7
View,
layout_width=-1,
layout_height="1.5dp",
backgroundColor=次要文字色,
},
{--8
FrameLayout,
layout_width=-1,
clickable=true,
padding="16dp",
{
FrameLayout,
layout_width=-1,
backgroundDrawable=
圆角(nil,0,{20,20,20,20,20,20,20,20})
.setStroke(3,文字色),
{
TextView,
foreground=波纹(波纹色,圆角(nil,-1,{20,20,20,20,20,20,20,20})),
layout_width=-1,
id="title",
padding="16dp",
textSize="16sp",
textColor=文字色,
gravity=17,
},
},
}
})

this.setContentView(loadlayout ({
  RelativeLayout,
  --layout_width="fill",
  --layout_height="fill",
  --visibility=4,
  backgroundColor=背景色,
  --orientation="vertical",
  --elevation="2%w",
  paddingTop=状态栏高度,
  {
    RelativeLayout,
    layout_width="fill",
    layout_height="56dp" ,
    gravity="center",
    padding="16dp",
    --backgroundColor=0xffffffff,
    elevation="2dp",
    paddingTop="8dp",
    paddingBottom="8dp",
    onClick=function()
      if topbar_click+2 > tonumber(os.time()) then
        list.smoothScrollToPosition(0,0)
       else
        topbar_click=tonumber(os.time())
      end
    end;
    {
      ImageView,
      src="drawable/back.png",
      layout_height="fill",
      layout_width="44dp",
      foreground=波纹(波纹色),
      onClick=function ()
        this.finish()
      end,
      padding="9dp",
      colorFilter=图标色,
    },
    
    {
      TextView,
      layout_width="fill",
      layout_height="fill",
      gravity="center",
      --layout_gravity="center",
      text="微件设置",
      textSize="20sp",
      textColor=文字色,
    },
  },
  
    {
      ListView,
      dividerHeight=0,
      layout_width=-1,
      layout_height=-1,
      paddingTop="56dp",
      clipToPadding=false,
      id="list",
      adapter=adp,
      onItemClickListener={onItemClick=function(p,v,i)
         if id==0 then
           toast("无效的微件 ID")
           return
         end
         v=v.getChildAt(1)
            local pop=PopupMenu(this,v)
        local menu=pop.Menu
        local function setText(a)
          --this.setDebug(true)
          --print(...)
          config:setClickAction(id,({
             打开闲言="windmill",
             制作壁纸="detail",
             刷新句子="refresh",
             无=""
          })[tostring (a)])
          initSettings()
        end
        menu.add("打开闲言").onMenuItemClick=setText
        menu.add("制作壁纸").onMenuItemClick=setText
        menu.add("刷新句子").onMenuItemClick=setText
        menu.add("无").onMenuItemClick=setText
        pop.show()
      end
      },
    },
  
  {
    LinearLayout,
    layout_width="fill",
    layout_height="56dp" ,
    foreground=上下渐变({背景色,深透,淡透}),
  },
}))

function initSettings()
local i=this.getIntent()
local preId=id
id=i.getIntExtra("appWidgetId",0)
if id==0 then
return
end
local d,d2
for _,v in ipairs(data) do
if v.__type==5 then
d=v
elseif d then
d2=v
break
end
end
if preId!=id then
--this.setDebug(true)
--print(dump(config:getIds()),tostring(id))
local isNew=config:isNewId(tostring(id))
--print(isNew,i.getAction(),tostring(id),dump(config:getIds()))
local isConfigure=i.getAction()=="android.appwidget.action.APPWIDGET_CONFIGURE"
&&isNew
local d2t=d2.__type
if isConfigure&&d2t==8 then
elseif isConfigure then
table.insert(data,2,{__type=8,
title={
text="应用",
onClick=function()
added=true
this.setResult(-1,
Intent()
.putExtra("appWidgetId",id)
)
.finish()
end
}
})
elseif isConfigure==false&&d2t==8
table.remove(data,2)
end
end
local style=config:getStyle(id)
local sen=config:getSentence(id)
local textSize=config:getTextSize(id)
local isVip=activity.getSharedData("vip_time")~=nil and activity.getSharedData("vip_time")~="已到期" and activity.getSharedData("vip_time")~="未开通"
if sen==""||!isVip then
d.title.text=isVip
&&"你若安好，便是晴天！"
||"成为会员即可查看"
d.source.visibility=8
d.source.text=nil
else
local sentence=json.decode(sen)
d.title.text=sentence.hitokoto
d.source.visibility=0
d.source.text=sentence.from
end
d.title.textSize=textSize--.."px"
d.source.textSize=textSize-2-- .."px"
if style==0 then
d.title.textColor=isAutoDark&&文字色||0xff000000
d.source.textColor=d.title.textColor
d.background.background=
isAutoDark&&ColorDrawable(0)
||圆角(nil,-1,{20,20,20,20,20,20,20,20})
elseif style==1 then
d.title.textColor=isAutoDark&&文字色||-1
d.source.textColor=d.title.textColor
d.background.background=
isAutoDark&&ColorDrawable(0)
||圆角(nil,0xff000000,{20,20,20,20,20,20,20,20})
else
d.title.textColor=文字色
d.source.textColor=d.title.textColor
d.background.background=ColorDrawable(0)
end
for _,v in ipairs(data) do
if v.__type==3 then
d=v
break
end
end
local backgroundChecked=圆角(nil,文字色,{20,20,20,20,20,20,20,20})
local backgroundUnchecked=圆角(nil,文字色-0xa0000000,{20,20,20,20,20,20,20,20})
if style==0 then
d.card1.background=backgroundChecked
d.card2.background=backgroundUnchecked
d.title1.textColor=文字色
d.title2.textColor=次要文字色
if !isAutoDark then
d.title3.textColor=次要文字色
d.card3.background=backgroundUnchecked
end
elseif style==1 then
d.title1.textColor=次要文字色
d.title2.textColor=文字色
d.card1.background=backgroundUnchecked
d.card2.background=backgroundChecked
if !isAutoDark then
d.title3.textColor=次要文字色
d.card3.background=backgroundUnchecked
end
else
d.title1.textColor=次要文字色
d.card1.background=backgroundUnchecked
if isAutoDark then
d.title2.textColor=文字色
d.title3.textColor=次要文字色
d.card2.background=backgroundChecked
d.card3.background=backgroundUnchecked
else
d.title3.textColor=文字色
d.title2.textColor=次要文字色
d.card2.background=backgroundUnchecked
d.card3.background=backgroundChecked
end
end
for _,v in ipairs(data) do
if v.__type==4 then
d=v
break
end
end
--this.setDebug(true)
--print(textSize)
d.action=textSize.." px"
d.progress.progress=textSize-14
for _,v in ipairs(data) do
if v.__type==6 then
d=v
break
end
end
d.action=(function(l)
if l=="windmill" then
return "启动闲言"
elseif l=="detail" then
return "制作壁纸"
elseif l=="refresh" then
return "刷新句子"
end
return "无"
end)(config:getClickAction(id))
adp.notifyDataSetChanged()
end

initSettings()

onNewIntent=initSettings

local broadCast=luajava.override(BroadcastReceiver,{onReceive=function(_,c,i)
local isThis
local ids=i.getIntArrayExtra("appWidgetIds")
for k=0,#ids-1 do
if ids[k]==id then
isThis=true
break
end
end
if !isThis then
return
end
local act=i.getAction()
if act=="windmill.widget.oneword.REMOVED"--[[&&isThis]] then
this.finish()
else--if --[[act=="windmill.widget.oneword.TEXT_UPDATED"&&]]isThis then
initPreview(i)
end
end})

require"receiverregister"(
this,
broadCast,
IntentFilter()
.addAction("windmill.widget.oneword.TEXT_UPDATED")
.addAction("windmill.widget.oneword.APPEARANCE_CHANGED")
.addAction("windmill.widget.oneword.REMOVED")
)

function onPause()
if id==0||this.getPackageName():find"and" then
return
end
for _,v in ipairs(data) do
if v.__type==8 then
if !added then
return
end
end
end
config:setPendingSentence(
id,config:getSentence(id)
)
this.sendBroadcast(
Intent()
.setAction("android.appwidget.action.APPWIDGET_UPDATE")
.setClass(this--[[.getApplication()]],
luajava.bindClass"com.windmill.widget.OneWordWidgetProvider"
)
.putExtra("appWidgetIds",int{id})
)
end

function onDestroy()
this.unregisterReceiver(broadCast)
end
